package com.dev.web.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dev.bbs.domain.BbsUser;
import com.dev.bbs.service.IBbsUserService;
import com.dev.common.annotation.Log;
import com.dev.common.core.controller.BaseController;
import com.dev.common.core.domain.AjaxResult;
import com.dev.common.core.page.TableDataInfo;
import com.dev.common.enums.BusinessType;
import com.dev.common.utils.poi.ExcelUtil;

/**
 * 社区用户Controller
 *
 * @author ruoyi
 * @date 2019-09-17
 */
@Controller
@RequestMapping("user")
public class BbsUserController extends BaseController
{
    private String          prefix = "user";

    @Autowired
    private IBbsUserService bbsUserService;

    @GetMapping()
    public String user()
    {
        return prefix + "/user";
    }

    @GetMapping("home/{userId}")
    public String home(@PathVariable(value = "userId", required = true) Long userId, Model model)
    {
        BbsUser bbsUser = bbsUserService.getUserWithInfoById(userId);
        model.addAttribute("bbsUser", bbsUser);
        return prefix + "/home";
    }

    /**
    * 查询社区用户列表
    */
    @RequiresPermissions("bbs:user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BbsUser bbsUser)
    {
        startPage();
        List<BbsUser> list = bbsUserService.selectBbsUserList(bbsUser);
        return getDataTable(list);
    }

    /**
     * 导出社区用户列表
     */
    @RequiresPermissions("bbs:user:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BbsUser bbsUser)
    {
        List<BbsUser> list = bbsUserService.selectBbsUserList(bbsUser);
        ExcelUtil<BbsUser> util = new ExcelUtil<BbsUser>(BbsUser.class);
        return util.exportExcel(list, "user");
    }

    /**
    * 新增社区用户
    */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存社区用户
     */
    @RequiresPermissions("bbs:user:add")
    @Log(title = "社区用户", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BbsUser bbsUser)
    {
        return toAjax(bbsUserService.insertBbsUser(bbsUser));
    }

    /**
     * 修改社区用户
     */
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Long userId, ModelMap mmap)
    {
        BbsUser bbsUser = bbsUserService.selectBbsUserById(userId);
        mmap.put("bbsUser", bbsUser);
        return prefix + "/edit";
    }

    /**
     * 修改保存社区用户
     */
    @RequiresPermissions("bbs:user:edit")
    @Log(title = "社区用户", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BbsUser bbsUser)
    {
        return toAjax(bbsUserService.updateBbsUser(bbsUser));
    }

    /**
    * 删除社区用户
    */
    @RequiresPermissions("bbs:user:remove")
    @Log(title = "社区用户", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(bbsUserService.deleteBbsUserByIds(ids));
    }
}
